<template>
  <view class="container">
    <view class="loading-content">
      <u-loading mode="circle" size="50"></u-loading>
      <view class="loading-tect-content">
        <text class="loading-text">加载中</text>
      </view>
    </view>
    <view class="btn-content">
      <text>如未成功跳转，</text>
      <text class="btn-click" @click="goFaceAuth">点击此处</text>
      <text>手动跳转</text>
    </view>
  </view>
</template>
<script lang="ts" setup>
import { onShow, onLoad } from '@dcloudio/uni-app';

const goFaceDone = ref(false); // 已跳转至公证签做人脸
const redirectUrl = ref('');
const bizToken = ref('');
const goFaceAuth = () => {
  wx.navigateToMiniProgram({
    appId: 'wx1cf2708c2de46337', // 公证签小程序APPID
    path: `/pages/face/index?bizToken=${bizToken.value}`, // 刷脸页面地址
    success() {
      goFaceDone.value = true;
    },
  });
};

onLoad((e: any) => {
  console.log(e, '----------');

  bizToken.value = e.bizToken;
  redirectUrl.value = decodeURIComponent(e.redirectUrl);
  goFaceAuth();
});

onShow(() => {
  // 防止从实名/意愿页进入后直接返回
  if (!goFaceDone.value) return;

  // 如果已经跳转过，重置
  goFaceDone.value = false;

  // getEnterOptionsSync 方法从基础库 2.9.4 开始支持，低版本需做兼容处理
  const options = wx.getEnterOptionsSync();
  console.log('---options', options);
  // 从公证签小程序返回
  if (
    options.scene === 1038 &&
    options.referrerInfo.extraData &&
    options.referrerInfo.extraData.faceResult
  ) {
    const pages = getCurrentPages();
    console.log(pages, 'pages');
    const pre: any = pages[pages.length - 2];
    console.log(pre, 'pre');
    // 重新加载实名页面
    if (pre.onLoad && typeof pre.onLoad === 'function') {
      // pre.reloadPage(redirectUrl.value + `&timeStamp=${new Date().getTime()}`)
      pre.onLoad({
        redirectUrl: redirectUrl.value + `&timeStamp=${new Date().getTime()}`,
      });
      wx.navigateBack({
        delta: 1,
      });
    }
  }
});
</script>
<style lang="scss">
.container {
  width: 100%;
  height: 100%;
  padding-top: 100rpx;
}
.loading-content {
  text-align: center;
  width: 100%;
}
.loading-tect-content {
  font-size: 40rpx;
  margin-top: 48rpx;
  color: #333;
}

.btn-content {
  font-size: 40rpx;
  color: #333;
  text-align: center;
  margin-top: 24rpx;
}
.btn-click {
  color: #095fe0;
}
</style>
